package com.demo.concorll;

import com.demo.bean.PersonMysql;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.InputStream;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@RestController
@RequestMapping(value = "/mysql")
public class MysqlDemo {
    @Resource
    private JdbcTemplate template;

    //手写SQL查询
    @RequestMapping(value = "/selectSqlAll")
    public String selectSqlAll() {

        String sql = "SELECT * FROM person";
        List<PersonMysql> list=template.query(sql, new RowMapper<PersonMysql>() {
            @Override
            public PersonMysql mapRow(ResultSet resultSet, int i) throws SQLException {
                PersonMysql personMysql =new PersonMysql();
                personMysql.setAge(resultSet.getInt("age"));
                personMysql.setName(resultSet.getString("name"));
                personMysql.setSex(resultSet.getString("sex"));
                return personMysql;
            }
        });
        StringBuffer sb = new StringBuffer();
        for (PersonMysql select : list) {
            sb.append(select.getName() + ">>>" + select.getAge() + ">>>" + select.getSex()+"\n");
        }
        return sb.toString();
    }

    @RequestMapping(value = "/selectMybatis")
    public String selectMybatis() {
        InputStream is = MysqlDemo.class.getResourceAsStream("/sqlMapConfig.xml");

        return "";
    }
}
